<?php
/**
 * Created by PhpStorm.
 * User: 70767
 * Date: 2020/5/6
 * Time: 23:41
 */

namespace app\common\logic;
use think\Db;

class StorehouseLogic
{
    /**
     * 仓库列表
     * @param $region_id  区域id
     * @param $city_id    城市id
     * @param $show_type 1.地图模式 2.列表模式
     * @param $keyword 关键字
     * @param $rent_status 1-在租  2-在建 3-满足  租的状态
     */
    public function store_house_list($region_id, $city_id, $show_type,$keyword='',$rent_status=0)
    {
        $where = [];
        if ($region_id) {
            $where['region_id'] = $region_id;
        }

        if ($city_id) {
            $where['city_id'] = $city_id;
        }

        if ($keyword) {
            $where['house_name'] = ['like', ['%' . $keyword . '%']];
        }

        if ($rent_status) {
            $where['rent_status'] = $rent_status;
        }

        $where['status'] = 1;
        $files = '';
        if ($show_type == 1) {
            $files = 'house_id,house_name,house_ename,long,lat,region_id,city_id,icon,address,telephone,site_area,rent_status';
        } else if ($show_type == 2) {
            $files = 'house_id,house_name,house_ename,long,lat,city_id,region_id,address,telephone,site_area,icon,rental_area,add_time,rent_status';
        } else {
            $files = 'house_id,house_name,house_ename,long,lat,region_id,city_id,icon,address,telephone,site_area,rent_status';
        }

        $result = Db::name('storehouse')->where($where)->field($files)->select();
        foreach ($result as $key => $value) {
            $result[$key]['icon'] = to_all_url($value['icon']);
        }
        return $result;
    }

    /**
     * 仓库详情
     * @param $house_id 仓库id
     * @return array
     */
    public function get_storehouse_msg($house_id)
    {
        $info = Db::name('storehouse')->where(['house_id' => $house_id, 'status' => 1])->select();

        if ($info && count($info)>0) {
            $banner = Db::name('storehouse_banner')->where(['storehouse_id' => $house_id, 'types' => 1])->field('id,img_url')->select();
            $images = Db::name('storehouse_banner')->where(['storehouse_id' => $house_id, 'types' => 2])->field('id,img_url')->select();
            $land = Db::query("select * from long_storehouse_landmark as b left join long_landmark as l on b.land_id = l.land_id where b.storehouse_id = ?", [$house_id]);
            $item = Db::query("select item_id,item_name,urls  from long_store_item as b left join long_item as l on b.item_id = l.id where b.store_id = ?", [$house_id]);
            $info[0]['exoneration'] = '';
            $conf_db = Db::name('conf')->where(['id' => 1])->find();
            if ($conf_db)
                $info[0]['exoneration'] = $conf_db['exoneration'];

            foreach ($banner as $key=>$value) {
                $banner[$key]['img_url'] = to_all_url($value['img_url']);
            }

            foreach ($images as $key=>$value) {
                $images[$key]['img_url'] = to_all_url($value['img_url']);
            }

            foreach ($land as $key=>$value) {
                $land[$key]['icon_url'] = to_all_url($value['icon_url']);
            }

            foreach ($item as $key=>$value) {
                $item[$key]['urls'] = to_all_url($value['urls']);
            }

            $info[0]['platform_img'] = to_all_url($info[0]['platform_img']);
            $info[0]['head_img'] = to_all_url($info[0]['head_img']);

        } else {
            $banner = [];
            $land = [];
            $images = [];
            $item = [];
        }

        return ['info' => $info, 'banner' => $banner, 'land' => $land, 'images' => $images, 'item' => $item];
    }

    public function getConfig()
    {
        $citys = Db::name('city')->where(['status'=>1])->select();
        $region = Db::name('region')->where(['status'=>1])->select();
        $host = '';
        return ['city' => $citys, 'region' => $region, 'host' => $host];
    }

    /**
     * 获得最近的
     * @param $long
     * @param $lat
     */
    public function get_nearest($long,$lat){
        $info = Db::name('storehouse')->where(['status'=>1])->select();
        $min_dic = -1;
        $cur_house = [];
        foreach ($info as $k=>$v) {
            $cur_dic = getdistance($long,$lat,$v['long'],$v['lat']);
            if($min_dic<0 || $cur_dic<$min_dic) {
                $min_dic = $cur_dic;
                $cur_house = $v;
            }
        }

        return $cur_house;
    }
}